package com.example.gallery;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Toast;

import com.example.board.R;



@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener {
	static final String TAG = "Login Activity";
	EditText et_id;
	EditText et_pw;
	CheckBox chk_login;
	CheckBox chk_pw;
	Button btn_login;
	Button btn_join;

	String l_id ;	
	String l_pw ;

	String id;
	String pw;
	String imei;
	String file_name;
	String path;
	Handler handler;
	ArrayList<MemberDTO> list;
	MemberDTO member;
	int fail = 0;
	int succ = 0;
	
	Common common;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, TAG);
		SharedPreferences prf = getSharedPreferences("login_info", 0);
		l_id = prf.getString("id", "");
		l_pw = prf.getString("pw", "");
		Log.i(TAG, l_id);
		if(("").equals(l_id) || ("").equals(l_pw)){	//�α��� ����X -> ����ȭ��
			setContentView(R.layout.main);
			et_id = (EditText) findViewById(R.id.et_id);
			et_pw = (EditText) findViewById(R.id.et_pw);
			chk_login = (CheckBox) findViewById(R.id.chk_login);
			chk_pw = (CheckBox) findViewById(R.id.chk_pw);
			btn_login = (Button) findViewById(R.id.btn_login);
			btn_join = (Button) findViewById(R.id.btn_join);

			chk_pw.setOnCheckedChangeListener(this);
			btn_login.setOnClickListener(this);																	
			btn_join.setOnClickListener(this);
		}
		else{	//�α��� ���� �� �Խ��� ����Ʈ�� �ٷ�
			Intent intent = new Intent(this, BoardListActivity.class);
			intent.putExtra("id", l_id);
			intent.addCategory(Intent.ACTION_MAIN);
			
			startActivity(intent);
			finish();
		}
		handler = new Handler();
	}

	private Runnable mrunnable = new Runnable() {

		@Override
		public void run() {
			btn_login.setEnabled(true);
			btn_login.setBackground(getResources().getDrawable(
					R.drawable.btn_active));
		}
	};
	
	private Runnable joinrun = new Runnable() {
		
		@Override
		public void run() {
			common.go(MainActivity.this, JoinActivity.class);
		}
	};
	@Override
	public void onClick(View v) {
		id = et_id.getText().toString();
		pw = et_pw.getText().toString();

		switch (v.getId()) {
		case R.id.btn_login:
			if ("".equals(id) || id == null) {
				Toast toast = Toast.makeText(getApplicationContext(),
						"ID�� �Է��� �ּ���", Toast.LENGTH_SHORT);
				toast.show();
				return;
			} else if ("".equals(pw) || pw == null) {
				Toast toast = Toast.makeText(getApplicationContext(),
						"��й�ȣ�� �Է��� �ּ���", Toast.LENGTH_SHORT);
				toast.show();
				return;
			} else {	//�α���
				MemberHandler db = MemberHandler.open(this);
				MemberDTO member = new MemberDTO(id, pw);
//				db.memberlist();
				succ = db.login(member);
				if(succ > 0){
					fail = 0;
					SharedPreferences pf = getSharedPreferences("login", 0);
					SharedPreferences.Editor login = pf.edit();
					login.putString("id", id);
					login.putString("pw", pw);
					login.commit();
					if (chk_login.isChecked() == true) {	//�α��� ����

						SharedPreferences prf = getSharedPreferences("login_info", 0);
						SharedPreferences.Editor login_info = prf.edit();

						login_info.putString("id", id);
						login_info.putString("pw", pw);
						login_info.commit();

						Log.i(TAG, "prf id : " + id);
						Log.i(TAG, "prf pw : " + pw);
					}
					Intent intent = new Intent(this, BoardListActivity.class);
					startActivity(intent);
					finish();
				}
				else{
					Toast.makeText(getApplicationContext(),
							"ID�� ��й�ȣ�� ��ġ���� �ʽ��ϴ�.\nȮ�� �� �ٽ� �õ� �� �ּ���\n3�� Ʋ�� �� 5�ʰ� ���", Toast.LENGTH_LONG).show();
					fail++;
					if(fail>=3){
						btn_login.setEnabled(false);
						btn_login.setBackground(getResources().getDrawable(
								R.drawable.btn_deactive));
						handler.postDelayed(mrunnable, 5000); // Runnable
																// ��ü�� 5�� ��
																// ����
						// handler.postAtFrontOfQueue(mrunnable); //Runnable
						// ��ü�� Queue �� �տ� �Ҵ�
						// handler.post(mrunnable); //Runnable ��ü ����

						fail = 0;
						return;
					}
				}
					Log.i(TAG, "���� ID : "+l_id);
					Log.i(TAG, "���� PW : "+l_pw);
			}
			break;
		case R.id.btn_join:
			Intent intent = new Intent(this, JoinActivity.class);
			startActivity(intent);
//			finish();
			break;
		default:
			break;

		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		switch (buttonView.getId()) {
		case R.id.chk_pw:
			if (isChecked) { // ��й�ȣ üũ ǥ�� �� ��� text�� ����
				et_pw.setTransformationMethod(HideReturnsTransformationMethod
						.getInstance());
				// et_pw.setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL);
				// //not Work
				Log.i(TAG, "checked");
			} else {
				et_pw.setTransformationMethod(PasswordTransformationMethod
						.getInstance());
				// et_pw.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
				// //not Work
				Log.i(TAG, "unchecked");
			}
			break;

		default:
			break;
		}
	}


}
